//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "FFTVarianceGammaEngine.h"
using namespace Cephei::QL::Experimental::Variancegamma;
#include <gen/QL/Experimental/Variancegamma/VarianceGammaProcess.h>
#include <gen/QL/StochasticProcess1D.h>
#include <gen/QL/Instrument.h>
#include <gen/QL/Experimental/Variancegamma/FFTEngine.h>
using namespace Cephei::QL;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Variancegamma::CFFTVarianceGammaEngine::CFFTVarianceGammaEngine (Cephei::QL::Experimental::Variancegamma::IVarianceGammaProcess^ process, Microsoft::FSharp::Core::FSharpOption<Double>^ logStrikeSpacing) : CFFTEngine(CFFTVarianceGammaEngine::typeid)
{
    CVarianceGammaProcess^ _Cprocess;
    try
    {
#ifdef HANDLE
        _phFFTVarianceGammaEngine = NULL;
#endif
        _Cprocess = safe_cast<CVarianceGammaProcess^> (process);
        _Cprocess->Lock();
        boost::shared_ptr<QuantLib::VarianceGammaProcess>& _process = static_cast<boost::shared_ptr<QuantLib::VarianceGammaProcess>&> (_Cprocess->GetShared ()); 
        QuantLib::Real _logStrikeSpacing = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (logStrikeSpacing) ? (QuantLib::Real)ValueHelper::Convert (logStrikeSpacing->Value) : 0.001); //4
        _ppFFTVarianceGammaEngine = new boost::shared_ptr<QuantLib::FFTVarianceGammaEngine> (new QuantLib::FFTVarianceGammaEngine ( _process,  _logStrikeSpacing ));
        SetFFTEngine (boost::dynamic_pointer_cast<QuantLib::FFTEngine> (*_ppFFTVarianceGammaEngine));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprocess != nullptr) _Cprocess->Unlock();
    }
}
Cephei::QL::Experimental::Variancegamma::CFFTVarianceGammaEngine::CFFTVarianceGammaEngine (boost::shared_ptr<QuantLib::FFTVarianceGammaEngine>& childNative, Object^ owner) : CFFTEngine(CFFTVarianceGammaEngine::typeid)
{
#ifdef HANDLE
	_phFFTVarianceGammaEngine = NULL;
#endif
	_ppFFTVarianceGammaEngine = &childNative;
    _ppFFTEngine = new boost::shared_ptr<QuantLib::FFTEngine> (boost::dynamic_pointer_cast<QuantLib::FFTEngine> (*_ppFFTVarianceGammaEngine));
}
Cephei::QL::Experimental::Variancegamma::CFFTVarianceGammaEngine::CFFTVarianceGammaEngine (QuantLib::FFTVarianceGammaEngine& childNative, Object^ owner) : CFFTEngine(CFFTVarianceGammaEngine::typeid)
{
#ifdef HANDLE
	_phFFTVarianceGammaEngine = NULL;
#endif
	_ppFFTVarianceGammaEngine = new boost::shared_ptr<QuantLib::FFTVarianceGammaEngine> (&childNative);
    _ppFFTEngine = new boost::shared_ptr<QuantLib::FFTEngine> (boost::dynamic_pointer_cast<QuantLib::FFTEngine> (*_ppFFTVarianceGammaEngine));
    _FFTVarianceGammaEngineOwner = owner;
    _FFTEngineOwner = owner;
}

Cephei::QL::Experimental::Variancegamma::CFFTVarianceGammaEngine::CFFTVarianceGammaEngine (CFFTVarianceGammaEngine^ copy) : CFFTEngine(CFFTVarianceGammaEngine::typeid)
{
#ifdef HANDLE
	_phFFTVarianceGammaEngine = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFFTVarianceGammaEngine = new boost::shared_ptr<QuantLib::FFTVarianceGammaEngine> (copy->GetShared());
        _ppFFTEngine = new boost::shared_ptr<QuantLib::FFTEngine> (boost::dynamic_pointer_cast<QuantLib::FFTEngine> (*_ppFFTVarianceGammaEngine));
    }
}
Cephei::QL::Experimental::Variancegamma::CFFTVarianceGammaEngine::CFFTVarianceGammaEngine (PLATFORM::Type^ t) : CFFTEngine(CFFTVarianceGammaEngine::typeid)
{
#ifdef HANDLE
	_phFFTVarianceGammaEngine = NULL;
#endif
	if (!t->IsSubclassOf(CFFTVarianceGammaEngine::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Variancegamma::CFFTVarianceGammaEngine::CFFTVarianceGammaEngine (QuantLib::Handle<QuantLib::FFTVarianceGammaEngine>& childNative, Object^ owner)  : CFFTEngine(CFFTVarianceGammaEngine::typeid)
{
	_phFFTVarianceGammaEngine = &childNative;
	_ppFFTVarianceGammaEngine = &static_cast<boost::shared_ptr<QuantLib::FFTVarianceGammaEngine>>(childNative.currentLink());
    _ppFFTEngine = new boost::shared_ptr<QuantLib::FFTEngine> (boost::dynamic_pointer_cast<QuantLib::FFTEngine> (*_ppFFTVarianceGammaEngine));
    _FFTVarianceGammaEngineOwner = owner;
}
Cephei::QL::Experimental::Variancegamma::CFFTVarianceGammaEngine::CFFTVarianceGammaEngine (QuantLib::Handle<QuantLib::FFTVarianceGammaEngine> childNative)  : CFFTEngine(CFFTVarianceGammaEngine::typeid)
{
	_phFFTVarianceGammaEngine = &childNative;
	_ppFFTVarianceGammaEngine = &static_cast<boost::shared_ptr<QuantLib::FFTVarianceGammaEngine>>(childNative.currentLink());
    _ppFFTEngine = new boost::shared_ptr<QuantLib::FFTEngine> (boost::dynamic_pointer_cast<QuantLib::FFTEngine> (*_ppFFTVarianceGammaEngine));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Variancegamma::CFFTVarianceGammaEngine::CFFTVarianceGammaEngine (QuantLib::FFTVarianceGammaEngine childNative)  : CFFTEngine(CFFTVarianceGammaEngine::typeid)
{
#ifdef HANDLE
	_phFFTVarianceGammaEngine = NULL;
#endif
	_ppFFTVarianceGammaEngine = new boost::shared_ptr<QuantLib::FFTVarianceGammaEngine> (new QuantLib::FFTVarianceGammaEngine (childNative));
    _ppFFTEngine = new boost::shared_ptr<QuantLib::FFTEngine> (boost::dynamic_pointer_cast<QuantLib::FFTEngine> (*_ppFFTVarianceGammaEngine));
}
#endif

Cephei::QL::Experimental::Variancegamma::CFFTVarianceGammaEngine::~CFFTVarianceGammaEngine ()
{
    if (_ppFFTVarianceGammaEngine != NULL)
    {
	    delete _ppFFTVarianceGammaEngine;
        _ppFFTVarianceGammaEngine = NULL;
    }
}
Cephei::QL::Experimental::Variancegamma::CFFTVarianceGammaEngine::!CFFTVarianceGammaEngine ()
{
    if (_ppFFTVarianceGammaEngine != NULL)
    {
	    delete _ppFFTVarianceGammaEngine;
    }
}
QuantLib::FFTVarianceGammaEngine& Cephei::QL::Experimental::Variancegamma::CFFTVarianceGammaEngine::GetReference ()
{
    if (_ppFFTVarianceGammaEngine == NULL) throw REFNEW NativeNullException ();
	return **_ppFFTVarianceGammaEngine;
}
boost::shared_ptr<QuantLib::FFTVarianceGammaEngine>& Cephei::QL::Experimental::Variancegamma::CFFTVarianceGammaEngine::GetShared ()
{
    if (_ppFFTVarianceGammaEngine == NULL) throw REFNEW NativeNullException ();
	return *_ppFFTVarianceGammaEngine;
}
QuantLib::FFTVarianceGammaEngine* Cephei::QL::Experimental::Variancegamma::CFFTVarianceGammaEngine::GetPointer ()
{
    if (_ppFFTVarianceGammaEngine == NULL) throw REFNEW NativeNullException ();
	return &**_ppFFTVarianceGammaEngine;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FFTVarianceGammaEngine>& Cephei::QL::Experimental::Variancegamma::CFFTVarianceGammaEngine::GetHandle ()
{
	if (_phFFTVarianceGammaEngine == NULL)
	{
		_phFFTVarianceGammaEngine = new Handle<QuantLib::FFTVarianceGammaEngine> (*_ppFFTVarianceGammaEngine);
	}
	return *_phFFTVarianceGammaEngine;
}
#endif
bool Cephei::QL::Experimental::Variancegamma::CFFTVarianceGammaEngine::HasNative () 
{
	return (_ppFFTVarianceGammaEngine != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Variancegamma::IFFTVarianceGammaEngine^ Cephei::QL::Experimental::Variancegamma::CFFTVarianceGammaEngine_Factory::Create (Cephei::QL::Experimental::Variancegamma::IVarianceGammaProcess^ process, Microsoft::FSharp::Core::FSharpOption<Double>^ logStrikeSpacing)
{
    return REFNEW CFFTVarianceGammaEngine ( process,  logStrikeSpacing);
}
